package com.zip.weather;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.zip.HeaderInterceptor;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class WRetrofit {
    public static final long READ_TIME_OUT = 30;
    public static final long CONNECT_TIME_OUT = 30;
    private static Retrofit mRetrofit;
    public static OkHttpClient sOkHttpClient;

    public static Retrofit getRetrofit(String baseUrl) {
        if (mRetrofit == null) {
            HttpLoggingInterceptor logInterceptor = new HttpLoggingInterceptor(new HttpLogger());
            logInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            OkHttpClient.Builder builder = new OkHttpClient().newBuilder()
                    .readTimeout(READ_TIME_OUT, TimeUnit.SECONDS)
                    .connectTimeout(CONNECT_TIME_OUT, TimeUnit.SECONDS)
                    //头部的统一处理
                    .addInterceptor(new HeaderInterceptor())
                    //日志的统一处理
                    .addInterceptor(logInterceptor);
            sOkHttpClient = builder.build();
            mRetrofit = new Retrofit.Builder().baseUrl(baseUrl)
                    .client(sOkHttpClient)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                    .build();
        }
        return mRetrofit;
    }


    public static Retrofit getRetrofit() {
        return getRetrofit("http://www.weather.com.cn/");
    }

    /**
     * 日志拦截
     */
    public static class HttpLogger implements HttpLoggingInterceptor.Logger {
        @Override
        public void log(String message) {
            Log.d("http", message);
        }
    }


    public void a() {


        Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
            }
        };
        Message msg = new Message();
        handler.dispatchMessage(msg);
    }
}

